<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CoSchoolExpress;
use App\Models\CoSchool;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Http\Request;

class CoSchoolExpressController extends AdminController
{
    protected $title = '学校快递点管理';
    protected $school_id;
    public function __construct(Request $request)
    {
        $this->school_id = $request->get('school_id',0);
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CoSchoolExpress(), function (Grid $grid) {
            if($this->school_id>0){
                $grid->model()
                    ->where("school_id",$this->school_id)
                    ->paginate(10);
            }
            $grid->id->sortable();
            $grid->name;
            $grid->column('school_id','学校名')->display(function ($v){
                return CoSchool::where('id',$v)->value('name');
            });
            $grid->status->switch();
            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            //关闭显示
            $grid->disableViewButton();
            $grid->actions(function ($actions) {
                $actions->disableEdit();
                //编辑按钮重构
                $url = '/admin/coSchoolExpress/'.$actions->getKey().'/edit?school_id='.$this->school_id;
                $actions->append("<a href=$url style='float: left'><i class='fa fa-edit'></i>>&nbsp;&nbsp;编辑</a>");
            });
            $grid->disableCreateButton();
            $grid->tools(function ($tools) {
                $url = '/admin/coSchoolExpress/create?school_id='.$this->school_id;
                $tools->append("<a href=$url class='btn btn-sm btn-success' style='float: right;'>
<i class='fa fa-save'></i>&nbsp;&nbsp;新增</a>");
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CoSchoolExpress(), function (Show $show) {
            $show->id;
            $show->name;
            $show->school_id;
            $show->status;
            $show->created_at;
            $show->updated_at;
            $show->disableListButton();
            $show->tools(function ($tools) {
                $url = '/admin/coSchoolBuilding?school_id='.$this->school_id;
                $tools->append("<a href=$url class='btn btn-sm btn-white'><i class='feather icon-list'></i><span class='d-none d-sm-inline'>&nbsp;列表</span></a>");
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CoSchoolExpress(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            if($this->school_id > 0){
                $CoSchool = new \App\Models\CoSchool();
                $choole=$CoSchool::all()->where('id',$this->school_id)->pluck('name', 'id');
                if($choole){
                    $form->select('school_id')
                        ->options($choole->toArray())->required();
                }
            }else{
                $form->select('school_id')
                    ->options(function ($school_id) {
                        $CoSchool = new \App\Models\CoSchool();
                        $data =  $CoSchool::all()->where('id',$school_id)->pluck('name', 'id');
                        return $data->toArray();
                    })
                    ->required();
            }
            $form->radio('status')->options(['1'=>'启用','0'=>'禁用'])->default(0);

            $form->display('created_at');
            $form->display('updated_at');
            $form->disableViewButton();
            $form->disableListButton();
//            $form->tools(function ($tools) {
//                $url = '/admin/coSchoolBuilding?school_id='.$this->school_id;
//                $tools->append("<a href=$url class='btn btn-sm btn-white'><i class='feather icon-list'></i><span class='d-none d-sm-inline'>&nbsp;列表</span></a>");
//            });
            $form->tools(function (Form\Tools $tools) {
                // 去掉跳转列表按钮
                $tools->disableList();
                // 去掉跳转详情页按钮
                $tools->disableView();
                // 去掉删除按钮
                $tools->disableDelete();
            });

            if($this->school_id > 0){
                $form->tools('<a href="/admin/coSchoolExpress?school_id='.$this->school_id .'" class="btn btn-sm btn-white "><i class="feather icon-list"></i><span class="d-none d-sm-inline">&nbsp;列表</span></a>');
            }else{
                $form->tools('<a href="/admin/coSchoolExpress?school_id='.$this->school_id .'" class="btn btn-sm btn-white "><i class="feather icon-list"></i><span class="d-none d-sm-inline">&nbsp;列表</span></a>');
            }
            $form->footer(function ($footer) {
                // 去掉`重置`按钮
                $footer->disableReset();

                // 去掉`查看`checkbox
                $footer->disableViewCheck();

                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();

                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });
        });
    }
}
